<template>
  <div>
    <!-- 搜索区域 -->
    <el-row>
      <el-form :inline="true">
        <el-form-item label="文章名称">
          <el-input
            clearable
            @clear="queryList"
            v-model="queryInfo.name"
            placeholder="请输入文章名称"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="queryList">搜索</el-button>
          <el-button type="primary" @click="toaddevaluation"
            >新增文章</el-button
          >
        </el-form-item>
      </el-form>
    </el-row>
    <!-- 表格区域 -->
    <el-table :data="evaluationList" border style="width: 100%">
      <el-table-column prop="id" label="编号"> </el-table-column>
      <el-table-column prop="name" label="评测名"> </el-table-column>
      <el-table-column prop="simplyIntroduce" label="简介">
        <template slot-scope="scope">
          <el-link type="primary" @click="toCheck(scope.row)">点击查看</el-link>
        </template>
      </el-table-column>
      <el-table-column prop="img" label="评测图片">
        <template slot-scope="scope">
          <el-image
            style="width: 100px; height: 100px"
            :src="
              'http://localhost:8888/sfzy/downLoad?filename=' + scope.row.img
            "
          >
          </el-image>
        </template>
      </el-table-column>
      <el-table-column prop="total" label="题目数量"> </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-link type="primary" @click="toEditEvaluation(scope.row)"
            >编辑评测</el-link
          >
          <el-link type="primary" @click="deleteEvaluation(scope.row.id)"
            >删除评测</el-link
          >
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页区域 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="queryInfo.pageNum"
      :page-sizes="[5, 10, 15, 20]"
      :page-size="queryInfo.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>
    <!-- 查看简介 -->
    <el-dialog :title="title" :visible.sync="dialogFormVisible1" @close="cancle">
      <el-form :model="evaluation">
        <el-form-item label="简介" label-width="80px">
          <!-- <el-input
            placeholder="请输入简介"
            v-model="evaluation.simplyIntroduce"
          ></el-input> -->
          <!-- 富文本编辑器组件 -->
          <!-- quill-editor标签 -->
          <quill-editor v-model="evaluation.simplyIntroduce" :disabled="true"></quill-editor>
        </el-form-item>
      </el-form>
    </el-dialog>

    <!-- 新增和修改的对话框 -->
    <el-dialog :title="title" :visible.sync="dialogFormVisible" @close="cancle">
      <el-form :model="evaluation">
        <el-form-item label="评测名" label-width="80px">
          <el-input
            placeholder="请输入评测名"
            v-model="evaluation.name"
          ></el-input>
        </el-form-item>
        <el-form-item label="简介" label-width="80px">
          <!-- <el-input
            placeholder="请输入简介"
            v-model="evaluation.simplyIntroduce"
          ></el-input> -->
          <!-- 富文本编辑器组件 -->
          <!-- quill-editor标签 -->
          <quill-editor v-model="evaluation.simplyIntroduce"></quill-editor>
        </el-form-item>
        <el-form-item label="评测图片" label-width="80px">
          <!-- <el-input placeholder="请输入评测图片" v-model="evaluation.img"> -->
          <el-upload
            action="http://localhost:8888/sfzy/upload"
            list-type="picture-card"
            :file-list="fileList"
            :on-success="handleSuccess"
            :limit="1"
            :on-preview="handlePictureCardPreview"
            :on-remove="handleRemove"
          >
            <i class="el-icon-plus"></i>
          </el-upload>
          <el-dialog :visible.sync="dialogVisible">
            <img width="100%" :src="dialogImageUrl" alt="" />
          </el-dialog>
          <!-- </el-input> -->
        </el-form-item>

        <el-form-item label="题目数量" label-width="80px">
          <el-input
            placeholder="请输入题目数量"
            v-model="evaluation.total"
          ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancle">取 消</el-button>
        <el-button type="primary" @click="addevaluation">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
export default {
  data() {
    return {
      queryInfo: {
        name: "",
        pageNum: 1,
        pageSize: 5,
      },
      evaluationList: [],
      total: 20,
      title: "",
      evaluation: {
        id: "",
        name: "",
        simplyIntroduce: "",
        img: "",
        total: "",
      },
      dialogFormVisible: false,
      dialogFormVisible1: false,
      dialogImageUrl: "", // 预览图片路径
      dialogVisible: false, // 预览图片的对话框
      fileList: [], //图片预览
    };
  },
  methods: {
    toCheck(row){
      this.dialogFormVisible1=true;
      this.title="简介";
      this.evaluation=row
    },
    // 处理上传成功的函数
    handleSuccess(response) {
      // 把上传后的图片路径，交给文章对象的img
      this.evaluation.img = response.message;
    },

    // 删除图片的事件
    handleRemove(file, fileList) {
      console.log(file, fileList);
      // 移除数据
      this.evaluation.img = "";
    },

    // 预览图片事件
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url;
      this.dialogVisible = true;
    },
    //删除
    deleteEvaluation(id) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.$http.delete("heartmark/" + id).then((resp) => {
            if (resp.data.status == 200) {
              this.$message.success(resp.data.message);
              this.queryList();
            } else {
              this.$message.error(resp.data.message);
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    //进入修改页面
    toEditEvaluation(row) {
      // 赋值
      this.evaluation = row;
      // 设置标题
      this.title = "修改文章";
      // 开启对话框
      this.dialogFormVisible = true;
      // 动态的获取图片的名称并帮到到fileList
      this.fileList = [
        {
          name: row.img,
          url: "http://localhost:8888/sfzy/downLoad?filename=" + row.img,
        },
      ];
    },
    //添加评测
    addevaluation() {
      if (this.evaluation.id) {
        // 有值为true  修改
        this.$http.put("heartmark", this.evaluation).then((resp) => {
          if (resp.data.status == 200) {
            // 消息提示
            this.$message.success("修改成功！");
            // 清空数据
            this.evaluation = {
              // 文章对象
              id: "",
              name: "",
              simplyIntroduce: "",
              img: "",
              total: "",
            };
            // 关闭对话框
            this.dialogFormVisible = false;
            // 重新查询
            this.queryList();
          } else {
            this.$message.error("接口异常！");
          }
        });
      } else {
        this.$http.post("heartmark", this.evaluation).then((resp) => {
          if (resp.data.status == 200) {
            // 消息提示
            this.$message.success("添加成功！");
            // 清空数据
            this.evaluation = {
              // 文章对象
              id: "",
              name: "",
              simplyIntroduce: "",
              img: "",
              total: "",
            };
            // 关闭对话框
            this.dialogFormVisible = false;
            // 重新查询
            this.queryList();
          } else {
            this.$message.error("接口异常！");
          }
        });
      }
      //  在取消函数，添加成功， 修改成功的函数中加入以下代码实现图片的清空
      this.fileList = [];
    },
    //取消清楚内容
    cancle() {
      this.dialogFormVisible = false;
      this.evaluation = {
        // 文章对象
        id: "",
        name: "",
        simplyIntroduce: "",
        img: "",
        total: "",
      };
      this.queryList();
      //  在取消函数，添加成功， 修改成功的函数中加入以下代码实现图片的清空
      this.fileList = [];
    },
    //进去新增窗口
    toaddevaluation() {
      this.title = "新增测评";
      this.dialogFormVisible = true;
    },
    handleSizeChange(size) {
      //   console.log("每页显示数量发生改变");
      this.queryInfo.pageSize = size;
      this.queryInfo.pageNum = 1;
      this.queryList();
    },
    handleCurrentChange(num) {
      //   console.log("页码发生改变");
      this.queryInfo.pageNum = num;
      this.queryList();
    },
    //查询
    queryList() {
      this.$http
        .get("heartmark", {
          params: this.queryInfo,
        })
        .then((res) => {
          if (res.data.status == 200) {
            this.evaluationList = res.data.data.list;
            this.total = res.data.data.total;
          } else {
            this.$message.error("查询失败");
          }
        });
    },
  },
  mounted() {
    this.queryList();
  },
};
</script>
<style>
.el-breadcrumb {
  margin-bottom: 15px;
  padding: 0 0 15px;
  border-bottom: 1px solid #ddd;
}

.el-pagination {
  margin-top: 15px;
}

.el-link {
  padding: 0 14px;
}
.ql-container {
  min-height: 200px;
}
</style>